package com.itboye.ihomebank.activity.key;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.OrderViewPagerAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.activity.key.fragment.FragmentGuanLiKey;
import com.itboye.ihomebank.activity.key.fragment.FragmentGuanLiPass;
import com.itboye.ihomebank.ttKey.Operation;
import com.itboye.ihomebank.util.UIAlertView;

import java.util.ArrayList;

import static com.itboye.ihomebank.activity.key.fragment.FragmentGuanLiPass.lockMac;

/**
 * 锁用户管理
 */
public class ActivityYongHu extends BaseOtherActivity {
    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv,save;

    TextView tv_key;//钥匙
    TextView tv_pass;//密码

    private ViewPager mPager;
    private int currIndex;// 当前页卡编号
    private float offset;// 图片移动的偏移量
    private ImageView image;
    private ArrayList<Fragment> fragmentList;
    private UIAlertView uiAlertView;
    public static boolean addLock = false;
    public String lockid,lockMac,leixing;
    @Override
    protected int layout_id() {
        return R.layout.fragment_yong_hu;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("用户管理");
        lockid= getIntent().getStringExtra("lockid");
        lockMac= getIntent().getStringExtra("lockMac");
        leixing= getIntent().getStringExtra("type");
        initTextView();
        initImage();
        initViewPager();
        IntentFilter intentFilter=new IntentFilter(UpdateKeyTimeActivity.UPDATE_KEY);
        registerReceiver(receiver,intentFilter);

    }
    BroadcastReceiver receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.save:
                uiAlertView = new UIAlertView(ActivityYongHu.this, "注意哦", "重置后所有密码失效\n(请保持蓝牙连接)", "是", "取消");
                uiAlertView.show();
                uiAlertView.setClicklistener(new UIAlertView.ClickListenerInterface() {
                    @Override
                    public void doLeft() {
                        uiAlertView.dismiss();
                        showProgressDialog("正在重置，请保持蓝牙和锁链接...",true);
                        KeyFragment.bleSession.setOperation(Operation.RESET_KEYBOARD_PASSWORD);
                        KeyFragment.bleSession.setLockmac(lockMac);
                        addLock = true;
                    }
                    @Override
                    public void doRight() {
                        uiAlertView.dismiss();
                    }
                });
                break;
            default:
                break;
        }
    }
    /*
     * 初始化ViewPager
     */
    public void initViewPager() {
        mPager = (ViewPager) findViewById(R.id.mPager);
        fragmentList = new ArrayList<>();
        Fragment paidanFragment = new FragmentGuanLiKey();
        Fragment weixiuFragment = new FragmentGuanLiPass();
        fragmentList.add(paidanFragment);
        fragmentList.add(weixiuFragment);

        Intent intent = getIntent();
        int i = intent.getIntExtra("index", 0);
        currIndex = i;
        //
        // //给ViewPager设置适配器
        mPager.setAdapter(new OrderViewPagerAdapter(
                getSupportFragmentManager(), fragmentList));

        mPager.setCurrentItem(i);// 设置当前显示标签页为第一页
        setTextColor(i + 1);

        tranSlate(i, 0);

        mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

    }

    // 平移动画
    public void tranSlate(int currentItem, long durationMillis) {
        float one = offset;
        Log.v("offest", "" + offset);
        Animation animation = new TranslateAnimation(currIndex * one,
                currentItem * one, 0, 0);// 平移动画
        Log.v("currentoffest", "" + currIndex * one);
        Log.v("currentItem", "" + currentItem * one);
        animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setDuration(durationMillis);// 动画持续时间0.2秒
        image.startAnimation(animation);// 是用ImageView来显示动画的
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private float one = offset;// 两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            tranSlate(arg0, 200);
            currIndex = arg0;
            int i = currIndex + 1;
            setTextColor(i);
        }
    }

    // 设置顶部条目颜色的变化
    public void setTextColor(int i) {
        switch (i) {
            case 1:
                save.setVisibility(View.GONE);
                tv_key.setTextColor(getResources().getColor(R.color.main_color));
                tv_pass.setTextColor(getResources().getColor(R.color.black));
                break;
            case 2:
                tv_key.setTextColor(getResources().getColor(R.color.black));
                tv_pass.setTextColor(getResources().getColor(R.color.main_color));
                if (leixing.equals("0")){
                    save.setVisibility(View.VISIBLE);
                    save.setText("重置密码");
                }
                break;

            default:
                break;
        }
    }

    // 初始化标签
    public void initTextView() {
        tv_key = (TextView) findViewById(R.id.tv_key);
        tv_pass = (TextView) findViewById(R.id.tv_pass);
        tv_key.setOnClickListener(new txListener(0));
        tv_pass.setOnClickListener(new txListener(1));
    }

    public class txListener implements View.OnClickListener {
        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mPager.setCurrentItem(index);
        }
    }

    /*
      * 初始化图片的位移像素
      */
    public void initImage() {
        image = (ImageView) findViewById(R.id.cursor);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        int screenH = dm.heightPixels;
        offset = screenW / 2.0f;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                (int) offset - 20, 2);
        image.setLayoutParams(params);
    }
}
